'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class MetaSearchByUinResponse
    Inherits MetaInformationResponse

    Public Sub New()
        MyBase.New(MetaResponseSubType.SearchUserFoundReply)
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Throw New NotImplementedException
    End Function


    Private _FoundUserUin As Integer
    Public Property FoundUserUin() As Integer
        Get
            Return _FoundUserUin
        End Get
        Set(ByVal value As Integer)
            _FoundUserUin = value
        End Set
    End Property


    Private _Nickname As String
    Public Property Nickname() As String
        Get
            Return _Nickname
        End Get
        Set(ByVal value As String)
            _Nickname = value
        End Set
    End Property

    Private _FirstName As String
    Public Property FirstName() As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            _FirstName = value
        End Set
    End Property

    Private _LastName As String
    Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
        End Set
    End Property

    Private _EmailAddress As String
    Public Property EmailAddress() As String
        Get
            Return _EmailAddress
        End Get
        Set(ByVal value As String)
            _EmailAddress = value
        End Set
    End Property

    Private _AutorizationRequired As Boolean
    Public Property AutorizationRequired() As Boolean
        Get
            Return _AutorizationRequired
        End Get
        Set(ByVal value As Boolean)
            _AutorizationRequired = value
        End Set
    End Property


    Private _Status As SearchUserStatus
    Public Property Status() As SearchUserStatus
        Get
            Return _Status
        End Get
        Set(ByVal value As SearchUserStatus)
            _Status = value
        End Set
    End Property

    Private _Gender As Byte
    Public Property Gender() As Byte
        Get
            Return _Gender
        End Get
        Set(ByVal value As Byte)
            _Gender = value
        End Set
    End Property

    Private _Age As Integer
    Public Property Age() As Integer
        Get
            Return _Age
        End Get
        Set(ByVal value As Integer)
            _Age = value
        End Set
    End Property


    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = MetaResponse.SizeFixPart + 2
        Dim dataSize As Integer

        Core.Kernel.Logger.Log("MetaSearchByUinResponse", TraceEventType.Information, "found at index {0}; total size: {1}", index, data.Count)

        If data(index) = &HA Then
            ' Search succeeded
            index += 1

            dataSize = ByteConverter.ToUInt16LE(data.GetRange(index, 2))
            index += 2

            Core.Kernel.Logger.Log("MetaSearchByUinResponse", TraceEventType.Information, "succeeded; data size: {0}", dataSize)

            _FoundUserUin = CInt(ByteConverter.ToUInt32LE(data.GetRange(index, 4)))
            index += 4

            _Nickname = ByteConverter.ToZeroBasedStringFromUInt16LEIndex(index, data)
            index += 2 + If(String.IsNullOrEmpty(_Nickname), 0, _Nickname.Length) + 1

            _FirstName = ByteConverter.ToZeroBasedStringFromUInt16LEIndex(index, data)
            index += 2 + If(String.IsNullOrEmpty(_FirstName), 0, _FirstName.Length) + 1

            _LastName = ByteConverter.ToZeroBasedStringFromUInt16LEIndex(index, data)
            index += 2 + If(String.IsNullOrEmpty(_LastName), 0, _LastName.Length) + 1

            _EmailAddress = ByteConverter.ToZeroBasedStringFromUInt16LEIndex(index, data)
            index += 2 + If(String.IsNullOrEmpty(_EmailAddress), 0, _EmailAddress.Length) + 1

            If data(index) = 0 Then _AutorizationRequired = True
            index += 1

            _Status = DirectCast(Convert.ToInt32(ByteConverter.ToUInt16LE(data.GetRange(index, 2))), SearchUserStatus)
            index += 2

            _Gender = data(index)
            index += 1

            _Age = ByteConverter.ToUInt16LE(data.GetRange(index, 2))
            index += 2
        Else
            Core.Kernel.Logger.Log("MetaSearchByUinResponse", TraceEventType.Information, "invalid data.")
        End If
    End Sub
End Class

Public Enum SearchUserStatus
    Online = 0
    Offline = 1
    NonWebaware = 2
End Enum